package org.dubbo.consumer2.controller;

import com.alibaba.dubbo.demo.DemoService;
import com.alibaba.dubbo.demo.RcmdService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 修正类名拼写错误
@RestController
@RequestMapping("/api")
@CrossOrigin
public class ComsumerController {

    // 明确指定使用的ApplicationConfig Bean名称
    @DubboReference(application = "uniqueApplicationConfig", check = false)
    private DemoService demoService;

    @DubboReference(application = "uniqueApplicationConfig", check = false)
    private RcmdService rcmdService;

//    @GetMapping("/recommend")
//    public String recommendPage() {
//        return "recommend";
//    }

    @GetMapping("/test")
    public Map<String, Object> testService() {
        Map<String, Object> result = new HashMap<>();
        try {
            String response = demoService.sayName("Test");
            result.put("success", true);
            result.put("data", response);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

//    @GetMapping("/getRcmdList")
    @GetMapping("/get-recommendation")
    public Map<String, Object> getRcmdList(@RequestParam("uid") String uid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<String> recommendations = rcmdService.getRcmdList(uid);
            result.put("success", true);
            result.put("data", recommendations);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @PostMapping("/batchRecommend")
    @CrossOrigin( methods = {RequestMethod.POST, RequestMethod.OPTIONS})
    public Map<String, Object> batchRecommend(@RequestBody List<String> userIds) {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, List<String>> batchResult = new HashMap<>();
            for (String uid : userIds) {
                batchResult.put(uid, rcmdService.getRcmdList(uid));
            }
            result.put("success", true);
            result.put("data", batchResult);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}